<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\Type;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class TypeController extends CommonController
{
    //get  admin/type   全部分类列表
    public function index(){
        
        $type_datas = Type::all();

        return view('admin/type/index',compact('type_datas'));
    }



    //post  admin/type  获取要添加的数据并存储到数据库
    public function store(){
        $input = Input::except('_token');
        //验证
        $rules = [
            'tname'=>'required'
        ];
        $messages = [
            'tname.required'=>"类型名称不能为空"
        ];

        $validator = Validator::make($input,$rules,$messages);
        if($validator->passes()){
            $re = Type::create($input);
            if($re){
                return redirect('admin/type/');
            }else{
                return back()->with('errors',"类型添加失败");
            }
        }else{
            return back()->withErrors($validator);
        }
    }

    //get. admin/type/create   添加页面的显示及数据分配
    public function create(){

        return view('admin.type.add');
    }

    //get admin/category/{category}
    public function show(){

    }

    //delete admin/type/{type}
    public function destroy($tid){
        $re = Type::where('tid',$tid)->delete();
        if($re){
            $data = [
                'status'=>1,
                'msg'=>"类型删除成功"
            ];
        }else{
            $data = [
                'status'=>0,
                'msg'=>"类型删除失败"
            ];
            
        }
        return $data;
    }

    //put admin/type/{type}
    public function update($tid){
        $input = Input::except('_token','_method');

        $re = Type::where('tid',$tid)->update($input);
        if($re){
            return redirect('admin/type/');
        }else{
            return back()->with('msg',"类型更新失败");
        }
    }

    //get admin/type/{type}/edit
    public function edit($tid){
        
        $type_data = Type::find($tid);
        
        return view('admin.type.edit',compact('type_data'));
    }
}
